<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>CSS - Grid Layout</title>
    <style>
      :root {
        --grid-width: 1140px;
        --gutter-vertical: 80px;
        --gutter-horizontal: 60px;
      }
      .row {
        margin: 0 auto;
        max-width: var(--grid-width);

        background-color: #eee;
      }

      /* 除了最后一个孩子之外的所有行 */
      .row:not(:last-child) {
        margin-bottom: var(--gutter-vertical);
      }
      /* 清除浮动 */
      .row::after {
        display: table;
        clear: both;

        content: "";
      }
      /* 以col-开头的class属性 */
      [class^="col-"] {
        float: left;

        background-color: orangered;
      }
      [class^="col-"]:not(:last-child) {
        margin-right: var(--gutter-horizontal);
      }

      .col-1-of-2 {
        width: calc((100% - var(--gutter-horizontal)) / 2);
      }
      .col-1-of-3 {
        width: calc((100% - 2 * var(--gutter-horizontal)) / 3);
      }
      .col-2-of-3 {
        width: calc(
          ((100% - 2 * var(--gutter-horizontal)) / 3) * 2 +
            var(--gutter-horizontal)
        );
      }
      .col-1-of-4 {
        width: calc((100% - 3 * var(--gutter-horizontal)) / 4);
      }
      .col-2-of-4 {
        width: calc(
          ((100% - 3 * var(--gutter-horizontal)) / 4) * 2 +
            var(--gutter-horizontal)
        );
      }
      .col-3-of-4 {
        width: calc(
          ((100% - 3 * var(--gutter-horizontal)) / 4) * 3 +
            var(--gutter-horizontal) * 2
        );
      }
      .grid-container {
        display: grid;

        margin: 12px auto;
        width: 1140px;

        column-gap: 12px;
      }
      .grid-child {
        background-color: deepskyblue;
      }
      .auto-fit {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
      }
      .auto-fill {
        grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
      }

      .grid {
        display: grid;

        margin: 0 auto var(--gutter-vertical); /* 设置行间距 */
        max-width: var(--grid-width);

        background-color: #eee;

        grid-template-columns: repeat(12, 1fr); /* 创建12列等宽网格 */
        gap: var(--gutter-horizontal); /* 设置列间距 */
      }
      .grid-item {
        background-color: orangered;
      }
      .col-3 {
        grid-column: span 3;
      }
      .col-4 {
        grid-column: span 4;
      }
      .col-6 {
        grid-column: span 6;
      }
      .col-8 {
        grid-column: span 8;
      }
      .col-9 {
        grid-column: span 9;
      }

      .container {
        display: grid;
        margin-bottom: 20px;
        width: 1000px;
        background-color: #ddd;
      }

      /* min-content 示例 */
      .min-content-example {
        grid-template-columns: min-content min-content min-content;
      }

      /* max-content 示例 */
      .max-content-example {
        grid-template-columns: max-content max-content max-content;
      }

      /* 固定宽度对比 */
      .fixed-example {
        grid-template-columns: 100px 100px 100px;
      }
      .item--1 {
        background-color: orangered;
      }
      .item--2 {
        background-color: yellowgreen;
      }
      .item--3 {
        background-color: blueviolet;
      }
    </style>
  </head>
  <body>
    <section class="grid-test">
      <div class="row">
        <div class="col-1-of-2">Col 1 of 2</div>
        <div class="col-1-of-2">Col 1 of 2</div>
      </div>
      <div class="row">
        <div class="col-1-of-3">Col 1 of 3</div>
        <div class="col-1-of-3">Col 1 of 3</div>
        <div class="col-1-of-3">Col 1 of 3</div>
      </div>
      <div class="row">
        <div class="col-1-of-3">Col 1 of 3</div>
        <div class="col-2-of-3">Col 2 of 3</div>
      </div>
      <div class="row">
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-1-of-4">Col 1 of 4</div>
      </div>

      <div class="row">
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-2-of-4">Col 2 of 4</div>
      </div>

      <div class="row">
        <div class="col-1-of-4">Col 1 of 4</div>
        <div class="col-3-of-4">Col 3 of 4</div>
      </div>
    </section>
    <section class="grid-test">
      <div class="grid-container auto-fit">
        <div class="grid-child">测试1</div>
        <div class="grid-child">测试2</div>
        <div class="grid-child">测试3</div>
      </div>

      <div class="grid-container auto-fill">
        <div class="grid-child">测试1</div>
        <div class="grid-child"></div>
        <div class="grid-child">测试3</div>
      </div>
    </section>

    <section class="grid-test">
      <div class="grid">
        <div class="grid-item col-6">1 of 2</div>
        <div class="grid-item col-6">2 of 2</div>
      </div>

      <!-- 3等分 -->
      <div class="grid">
        <div class="grid-item col-4">1 of 3</div>
        <div class="grid-item col-4">2 of 3</div>
        <div class="grid-item col-4">3 of 3</div>
      </div>

      <!-- 1/3 + 2/3 -->
      <div class="grid">
        <div class="grid-item col-4">1 of 3</div>
        <div class="grid-item col-8">2 of 3</div>
      </div>

      <!-- 4等分 -->
      <div class="grid">
        <div class="grid-item col-3">1 of 4</div>
        <div class="grid-item col-3">2 of 4</div>
        <div class="grid-item col-3">3 of 4</div>
        <div class="grid-item col-3">4 of 4</div>
      </div>

      <!-- 1/4 + 1/4 + 2/4 -->
      <div class="grid">
        <div class="grid-item col-3">1 of 4</div>
        <div class="grid-item col-3">2 of 4</div>
        <div class="grid-item col-6">3 of 4</div>
      </div>

      <!-- 1/4 + 3/4 -->
      <div class="grid">
        <div class="grid-item col-3">1 of 4</div>
        <div class="grid-item col-9">3 of 4</div>
      </div>
    </section>

    <section class="grid-test">
      <!-- min-content-example / max-content-example / fixed-example-->
      <div class="container max-content-example">
        <div class="item item--1">这是一个相对较长的文本内容</div>
        <div class="item item--2">短文本</div>
        <div class="item item--3">
          supercalifragilisticexpialidocious（超长单词）
        </div>
      </div>
    </section>
  </body>
</html>
